import 'package:flutter/material.dart';
import 'package:flutter_app/view/sub_page/songs/widget/songs_card.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import 'logic.dart';
import 'state.dart';

class SongsPage extends StatelessWidget {
// 独立的滚动视图 防止tab切换时触发下拉刷新
  ScrollController _scrollController = ScrollController();

  final SongsLogic logic = Get.put(SongsLogic());
  final SongsState state = Get.find<SongsLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //FutureBuilder 加载成功在渲染页面
      body: FutureBuilder(
        future: logic.getData(),
        builder: (BuildContext context, AsyncSnapshot snapShot) {
          if (snapShot.connectionState == ConnectionState.waiting) {
            return Text('loading...');
            // ignore: missing_return
          } else if (snapShot.connectionState == ConnectionState.done) {
            // print(snapShot.hasError);
            // print('data:${snapShot.data}');
            if (snapShot.hasError) {
              return Text('Error: ${snapShot.error}');
            }
            return EasyRefresh(
                // 开启首次加载刷新
                firstRefresh: true,
                // 加载时的loading
                // 空视图、异常视图
                enableControlFinishRefresh: false,
                enableControlFinishLoad: true,
                controller: logic.controller,
                header: ClassicalHeader(),
                footer: ClassicalFooter(),
                onRefresh: () async {
                  //下拉请求新数据
                  logic.onRefresh();
                },
                onLoad: () async {
                  logic.onLoad();
                },
                child: Column(
                  children: [
                    Text(logic.count.toString()),
                    GetBuilder<SongsLogic>(
                        builder: (_) => SongCard(
                              scrollController: _scrollController,
                              songData: logic.songList,
                              //条目点击事件
                              onItem: (String msg) => logic.show(msg),
                            ))
                  ],
                ));
          } else {
            return Text('fail...');
          }
        },
      ),
    );
  }
}
